package com.scott.crm.role.service.impl;

import com.scott.crm.role.bean.RoleBean;
import com.scott.crm.role.dao.RoleDao;
import com.scott.crm.role.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 黄后林
 * @version 1.0
 * @note
 * @date 2021-11-27 16:23:39
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao dao;



    @Override
    public List<RoleBean> findRoleListByUserId(Integer userId) {
        return dao.findRoleListByUserId(userId);
    }

    @Override
    public List<RoleBean> findAllRoles() {
        return dao.selectAll();
    }

    @Override
    public List<RoleBean> findRoleList(Integer begin,Integer end) {
        List<RoleBean> roleList = dao.findRoleList(begin,end);
        return roleList;
    }

    @Override
    public int addRole(RoleBean roleBean) {

        roleBean.setToken("");
        RoleBean role = dao.selectOne(roleBean);
        if(role== null){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date  =df.format(new Date());
            roleBean.setCreateTime(date);
            roleBean.setLastUpdateTime(date);
            int counter = dao.addRole(roleBean);
            return counter;
        }
        return 0;
    }

    @Override
    public int updateRole(RoleBean role) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date  =df.format(new Date());
        role.setLastUpdateTime(date);
        return dao.updateByPrimaryKeySelective(role);
    }

    @Override
    public int deleteRoles(List<Integer> ids) {

        int counter=0;
        for(Integer id:ids){
            RoleBean role = new RoleBean();
            role.setId(id);
            int i = dao.deleteByPrimaryKey(role);
            counter=counter+i;
        }
        return counter;
    }
}
